package com.androturka.billintur.cyberpark;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import android.os.Bundle;
import com.androturka.cyberpark.R;
 
public class AlarmReceiver extends BroadcastReceiver {
 
	Notification notification;
	NotificationManager 	manager;
	int 					notificationID 		= 1;
	int 					notificationFlag	= 0;

	@Override
	public void onReceive(Context context, Intent intent) {
		/*
		try {
			Bundle bundle = intent.getExtras();
			String message = bundle.getString("alarm_message");
			Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
		*/
    	YemekhaneDB DB = YemekhaneDB.getInstance(context);
    	//DB.deleteAllMenus();
		MenuDownloader downloader = new MenuDownloader(context);		
		downloader.download();
		
		downloader.setAlarm();
		try {
		     Bundle bundle = intent.getExtras();
		     String message = bundle.getString("alarm_message");
		     Log.d(Common.TAG,message);
		     
		     if ( message.equals("notify") ){

		        //YemekhaneDB DB = new YemekhaneDB(context);
		     	//SQLiteDatabase dbObject = DB.getWritableDatabase();		
		     	//DB.onCreate(dbObject);
		     	
		    	 
		     	DailyMenu menu = DB.getMenu(Common.getDateStr());
		     	if ( menu != null ){

		     		Log.d(Common.TAG, menu.toString());
			     	
			 		manager = (NotificationManager) context
							.getSystemService(Context.NOTIFICATION_SERVICE);
					notification = new Notification(R.drawable.icon, "",
							System.currentTimeMillis());
	
					notificationID = 0;
					
					/*
					RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
					contentView.setImageViewResource(R.id.image, R.drawable.icon);
					
					String menuStr = menu.getCorba() + ", " +
									 menu.getAnayemek1() + "/ " +
									 menu.getAnayemek2() + "/ " +
									 menu.getVejeteryan() + ", " +
									 menu.getPilav() + "/ " +
									 menu.getMakarnaborek() + "/ " +
									 menu.getZeytinyagli() + ", " +
									 menu.getTatli1() + "/ " +
									 menu.getTatli2() + "/ " +
									 menu.getMeyve();
					contentView.setTextViewText(R.id.menu, menuStr);
					*/
					
					//contentView.setTextViewText(R.id.corba, "Çorba - Minestrone Çorba");
					//contentView.setTextViewText(R.id.anayemek1, "Ana Yemek 1 - Hasanpaşa Köfte");
					//contentView.setTextViewText(R.id.anayemek2, "Ana Yemek 2 - Etli Kuru Fasulye");
					//contentView.setTextViewText(R.id.vejeteryan, "Vejeteryan - Vej.Pizza");
	
					
					
					Intent i = new Intent(context, YemekhaneActivity.class);
				    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
	
					PendingIntent contentIntent = PendingIntent.getActivity(context,
							0, i, 0);

					notification.contentIntent = contentIntent;
					
					notification.setLatestEventInfo(context, "Cyberpark Yemekhanesi",
							"Yemeğiniz hazır sizi bekliyor", contentIntent);
				
					//notificationFlag |= Notification.DEFAULT_SOUND;

					//if ( confManager.getVibrationNofitication() )
						//notificationFlag |= Notification.DEFAULT_VIBRATE;
										
					//notificationFlag |= Notification.DEFAULT_LIGHTS;

					//notificationFlag |= Notification.FLAG_AUTO_CANCEL;

					//notification.defaults |= notificationFlag;
					notification.defaults = Notification.DEFAULT_SOUND | 
							 				Notification.DEFAULT_VIBRATE | 
						 					Notification.DEFAULT_LIGHTS;

					notification.flags = Notification.FLAG_AUTO_CANCEL;

					notification.number = notificationID;

					notification.ledARGB = 0xff00ff00;
					notification.ledOnMS = 300;
					notification.ledOffMS = 1000;


					//notification.contentView = contentView;
					manager.notify(notificationID, notification);
		    	 
		     	}
		     	
		     	String alarmTimeStr = DB.getNotificationTime();
		     	if ( alarmTimeStr != null ){
		     		
		     	}
		     		
		     }
		    	
		     /*
			Intent newIntent = new Intent(context, YemekhaneActivity.class);
			newIntent.putExtra("alarm_message", message);
			newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(newIntent);
			*/
		    } catch (Exception e) {
		     Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
		     e.printStackTrace();
		 
		    }
	}	
}